package weaver.formmode.customjavacode.customsearch;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wkj
 * Date: 2020/4/11
 * Time: 9:25
 * Description: 百货——权限控制
 */
public class CustomSearchBHQXKZ extends AbstractCustomSqlConditionJavaCode {
    @Override
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet recordSet = new RecordSet();

        String customid = (String) param.get("customid");
        User user = (User) param.get("user");
        int userid = user.getUID();
        baseBean.writeLog("generateSqlCondition——userid:" + userid + ";customid:" + customid);

        String sqlCondition = " (1=0 ";

        //百货权限控制表
        String BHQXKZTablename = baseBean.getPropValue("Mode_Qxkz", "BHQXKZTablename");

        if (userid == 1) {
            //系统管理员和数据管理员能看到全部数据
            sqlCondition = " 1=1 ";
            baseBean.writeLog("CustomSearchBHQXKZ——sqlCondition:" + sqlCondition);
            return sqlCondition;
        }

        //遍历   百货权限控制表
        rs.execute("select * from " + BHQXKZTablename);
        while (rs.next()) {
            String js = Util.null2String(rs.getString("js"));//角色
            String bmbm = Util.null2String(rs.getString("bmbm"));//部门编码
            String yt = Util.null2String(rs.getString("yt"));//业态
            String customids = Util.null2String(rs.getString("cxid"));//该角色拥有的查看权限

            boolean flag = false;
            String[] customidss = customids.split(",");
            for (String str : customidss) {
                if (str.equals(customid)) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                //获取角色中的人员
                recordSet.execute("select * from hrmrolemembers where roleid=" + js + " and resourceid=" + userid);
                if (recordSet.next()) {
                    if (!"".equals(bmbm) && bmbm != null) {
                        sqlCondition += " or t1.sqbmbm in (" + bmbm + ") ";
                    }
                    if (!"".equals(yt) && yt != null) {
                        sqlCondition += " or t1.sqbmssyt in (" + yt + ") ";
                    }
                }
            }
        }
        sqlCondition += ") ";
        baseBean.writeLog("CustomSearchBHQXKZ——sqlCondition:" + sqlCondition);
        return sqlCondition;
    }
}
